package com.hua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hua.common.BaseTool;
import com.hua.common.CustomError;
import com.hua.entities.R;
import com.hua.entities.ShoppingCart;
import com.hua.entities.User;
import com.hua.service.serviceImpl.ShoppingCartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

@RestController
@RequestMapping("/shoppingCart")
public class ShopingCartController {

    @Autowired
    private ShoppingCartServiceImpl service;

    @PostMapping("/add")
    public R<ShoppingCart> addFood(@RequestBody  ShoppingCart shoppingCart){
        Long userId = BaseTool.getInfo();
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> wrapper
                =new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);

        if(shoppingCart.getDishId()!=null){
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart dish = service.getOne(wrapper);

        if (dish!=null){
            Integer number = dish.getNumber();
            dish.setNumber(number+1);
            service.updateById(dish);

        }else {
            shoppingCart.setNumber(1);
            service.save(shoppingCart);
            dish=shoppingCart;
        }

        return R.success(dish);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> getCartInfo(){
        Long userId = BaseTool.getInfo();

        List<ShoppingCart> shoppingGoodsList
                = service.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, userId));

//        shoppingGoodsList.forEach(System.out::println);


        return R.success(shoppingGoodsList);
    }

    @DeleteMapping("/clean")
    public R<String>clearShoppingCart(){
        Long userId = BaseTool.getInfo();
        System.out.println(userId);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        service.remove(wrapper);
        return R.success("清空成功~~~");
    }

    @PostMapping("/sub")
    public R<String> decreaseGood(@RequestBody ShoppingCart shoppingCart){
//        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        if(shoppingCart.getDishId()!=null){
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        if(shoppingCart.getSetmealId()!=null){
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        wrapper.eq(ShoppingCart::getUserId,BaseTool.getInfo());
        ShoppingCart decrease = service.getOne(wrapper);
        Integer number = decrease.getNumber();
        decrease.setNumber(number-1);
        if(decrease.getNumber()==0){
            service.removeById(decrease);
            return R.success("减少成功~~~~");
        }
        service.updateById(decrease);
        return R.success("减少成功~~~~");
    }
}
